﻿Public Class AJAXLobby
    Inherits System.Web.UI.Page
    Dim sql As String
    Dim objDB As DBConnect
    Dim ds As New DataSet
    Dim userID As String
    Dim gameID As Integer
    Dim gameName As String
    Dim matchNum As Integer
    Dim totalMatches As Integer
    Dim message As String
    Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
        message = "Welcome to the Server... the current time and date is: "

        lblM.Text = message + DateTime.Now.ToString()


        Dim objdb As New DBConnect

        Dim userID As String = Session("userID")
        Name.Text = "userid??"
        lblTurn.Text = "tBoolean"


        If Not IsPostBack Then
            sql = "SELECT * FROM Game"
            ds = objdb.getDataSet(sql)
            ddlGameType.DataSource = ds
            ddlGameType.DataTextField = "GameName"
            ddlGameType.DataValueField = "GameID"
            ddlGameType.DataBind()

            Dim newSQL As String = "SELECT * FROM Match"
            ds = objdb.getDataSet(newSQL)

            gvMatches.DataSource = ds
            gvMatches.DataBind()
            ddlGameType_SelectedIndexChanged(sender, e)

        End If

    End Sub




    'Protected Sub btnJoin_Click(ByVal sender As Object, ByVal e As EventArgs) Handles btnJoin.Click

    '    Dim matchNum As Integer

    '    'rdbJoinGame
    '    For i As Integer = 1 To 6
    '        Dim isChecked As RadioButton = CType(gvMatches.Rows(i).Cells(2).FindControl("rdbJoinGame"), RadioButton)
    '        matchNum = gvMatches.Rows(i).Cells(0).Text
    '        If isChecked.Checked Then
    '            Dim sqlString As String = "INSERT INTO Match_Player '" + matchNum + "', '" + Session("userID") + "'"
    '            objDB.doUpdate(sqlString)
    '        End If
    '    Next
    'End Sub

    Protected Sub Timer1_Tick(ByVal sender As Object, ByVal e As System.EventArgs) Handles Timer1.Tick
        lblM.Text = message + DateTime.Now.ToString()
    End Sub

    'Protected Sub stuffChanges(ByVal sender As Object, ByVal e As EventArgs) Handles ddlGameType.SelectedIndexChanged
    '    gvMatches.Visible = True

    '    btnJoin.Visible = True


    '    sql = "SELECT * FROM Match WHERE gameID=" & gameID
    '    ds = objDB.getDataSet(sql)
    '    totalMatches = ds.Tables(0).Rows.Count


    '    Dim counter2 As Integer
    '    Dim intCount As Integer
    '    gvMatches.DataSource = ds
    '    gvMatches.DataBind()
    '    Dim searchDS As New DataSet
    '    For counter As Integer = 0 To totalMatches - 1
    '        Dim matchID As String = gvMatches.Rows(counter).Cells(0).Text
    '        Dim newString As String = "SELECT * FROM Match_Player WHERE MatchID='" + matchID + "'"
    '        searchDS = objDB.getDataSet(newString)
    '        counter2 = searchDS.Tables.Count()
    '        intCount = searchDS.Tables(0).Rows.Count
    '        gvMatches.Rows(counter).Cells(1).Text = intCount
    '    Next

    'End Sub

    

    Protected Sub Timer2_Tick(ByVal sender As Object, ByVal e As EventArgs) Handles Timer2.Tick
        lblUpdate.Text = "Matches last updated at: " + DateTime.Now.ToString()
        ddlGameType_SelectedIndexChanged(sender, e)


    End Sub

    Protected Sub ddlGameType_SelectedIndexChanged(ByVal sender As Object, ByVal e As EventArgs) Handles ddlGameType.SelectedIndexChanged

        lblNoGame.Text = ""
        Dim objdbb As New DBConnect
        Dim gamesname As String
        Dim gamerID As String
        gamerID = ddlGameType.SelectedValue

        gamesname = ddlGameType.SelectedItem.Text
        sql = "SELECT * FROM Match WHERE gameID=" & gamerID
        ds = objdbb.getDataSet(sql)
        totalMatches = ds.Tables(0).Rows.Count
        Dim counter2 As Integer
        Dim intCount As Integer
        gvMatches.DataSource = ds
        gvMatches.DataBind()
        Dim searchDS As New DataSet
        For counter As Integer = 0 To totalMatches - 1
            Dim matchID As String = gvMatches.Rows(counter).Cells(0).Text
            Dim newString As String = "SELECT * FROM Match_Player WHERE MatchID='" + matchID + "'"
            searchDS = objdbb.getDataSet(newString)
            counter2 = searchDS.Tables.Count()
            intCount = searchDS.Tables(0).Rows.Count
            gvMatches.Rows(counter).Cells(1).Text = intCount
        Next
        gvMatches.Visible = True
        If gvMatches.Rows.Count < 1 Then

            lblNoGame.Text = "THERE ARE NO MATCHES BASED ON THIS GAME AT THIS TIME"
        End If




    End Sub

 
End Class

